package com.awesumgames.awesum.sprites;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.awesumgames.awesum.Game;
import com.awesumgames.awesum.AwesumGlobal.Awesum;

public class Sprite /*extends SpriteBase*/ {
	
	
	public static void addSet(int resFile, int defsFile) {
		int texture = Game.Textures.load(resFile);
		
		
		JSONObject level = null;
		try {
			level = Awesum.Load.Json(defsFile);
			if (level != null) {
				JSONArray jArr = null;
				JSONObject jObj = null;
				JSONArray pos = null;
				SpriteSet set = new SpriteSet(resFile);
				
				//_levelName = level.getString("name");  
				
				//BLOCK
				jArr = level.getJSONArray("block");
				for (int i = 0; i < jArr.length(); ++i) {
					jObj = jArr.getJSONObject(i);
					
					pos = jObj.getJSONArray("pos");
					//String t = jObj.getString("type");
					set.defineSprite(Integer.parseInt(jObj.getString("id")), (float)pos.getDouble(0), (float)pos.getDouble(1), (float)pos.getDouble(2), (float)pos.getDouble(3));
					//Block.Type type;
					
					//if (t.equals("b")) type = Block.Type.BOOST;
					//else if (t.equals("sb")) type = Block.Type.SUPERBOOST;
					//else if (t.equals("r")) type = Block.Type.REDUCE;
					//else if (t.equals("br")) type = Block.Type.BREAK;
					//else if (t.equals("s")) type = Block.Type.SLIDE;
					//else type = Block.Type.NORMAL;
					//add(new Block(type,
					//		new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))     ));
				}
				
				//BALL
				jArr = level.getJSONArray("ball");
				for (int i = 0; i < jArr.length(); ++i) {
					//Log.d("debug", "added ball");
					jObj = jArr.getJSONObject(i);
					pos = jObj.getJSONArray("pos");
					//add(ball = new Ball(new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}
				//test = ball.pos;
			}

			JSONObject defs = Awesum.Load.Json(defsFile);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/*
	//STATIC VERTICES/INDICES: These are for sprites with 0-1 X&Y and S&T -----
	//					  coordinatess, uses the same VBO for all of them
	public static final FloatBuffer staticVertices = Awesum.makeFloatBuffer(new float[]{
			0f,	0f,	0f,		0f, 0f,
			1f,	0f,	0f,		1f, 0f,
			1f,	1f,	0f,		1f, 1f,
			0f,	1f,	0f,		0f, 1f
	});
	public static final ByteBuffer staticIndices = Awesum.makeByteBuffer(new byte[]{
			0, 1, 2,	2, 3, 0	
	});
	public static int staticVerticesVbo = -1;
	public static int staticIndicesVbo = -1;
	//-------------------------------------------------------------------------
	
	public Sprite(int resFile) {
		_file = resFile;
	}
	
	public void load() {
		//final Context context, final int resourceId)
		Awesum.debug("sprite load begin");
		if (staticVerticesVbo == -1) {
			final int buffers[] = new int[2];
			GLES20.glGenBuffers(2, buffers, 0);
			GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
			GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, staticVertices.capacity() * Game.FloatSize, staticVertices, GLES20.GL_STATIC_DRAW);
			
			GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
			GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, staticIndices.capacity(), staticIndices, GLES20.GL_STATIC_DRAW);
			//GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
			staticVerticesVbo = buffers[0];
			staticIndicesVbo = buffers[1];
		}
		
		super.load();
		
		Awesum.debug("tex = " + texture + ", vert vbo = " + staticVerticesVbo + ", ind vbo = " + staticIndicesVbo);
		Awesum.debug("sprite load end");
	}
	
	@Override
	public void draw() {
		if (!Game.relative)
			Matrix.setIdentityM(Game.modelMatrix, 0);
		Matrix.translateM(Game.modelMatrix, 0, pos.x, pos.y, pos.z);
		
		//Matrix.translateM(Game.modelMatrix, 0, translate.x, translate.y, translate.z);
		//Matrix.scaleM(Game.modelMatrix, 0, scale.x, scale.y, scale.z);
		if (!quat.isIdentity()) {
			//Vector3 axis = quat.axis();
			//Matrix.rotateM(Game.modelMatrix, 0, quat.angleDeg(), axis.x, 1f, axis.z);
		}
		
		//mvp
		GLES20.glUniformMatrix4fv(Game.Shaders.current.mvp, 1, false, Game.getMVP(), 0);
		
		//texture
		GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
        GLES20.glUniform1i(Game.Shaders.current.texture, 0);

        //vertices/tex coords
		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, staticVerticesVbo);
		GLES20.glVertexAttribPointer(Game.Shaders.current.pos, 3, GLES20.GL_FLOAT, false, 5 * Game.FloatSize, 0);
		GLES20.glVertexAttribPointer(Game.Shaders.current.texCoord, 2, GLES20.GL_FLOAT, false, 5 * Game.FloatSize, 3 * Game.FloatSize);

		//indices
		GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, staticIndicesVbo);
		GLES20.glDrawElements(GLES20.GL_TRIANGLES, staticIndices.capacity(), GLES20.GL_UNSIGNED_BYTE, 0);
		
		//unbind
		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
		GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
		
		super.draw();
	}
	
	@Override
	public void step() {
		super.step();
	}*/
}

